#include "SDL.h"
#include "SDL_opengl.h"
#include "SDL_image.h"
#include <string>
int rez=0;
int h = 600;//visochina na ekrana
int w = 800;//duljina na ekrana
int x=270,y=370;
int mapX=0,mapY=0;
GLuint loadTexture( const std::string &fileName )
{
    SDL_Surface *image = IMG_Load( fileName.c_str() );

    SDL_DisplayFormatAlpha(image);
    unsigned object(0);

    glGenTextures(1, &object);

    glBindTexture(GL_TEXTURE_2D, object);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);


    SDL_FreeSurface(image);

    return object;//vrutame snimkata
}
void Map(unsigned int texture)///Map-768*4///myMap-1324-702
{
    glColor4ub(255,255,255,255);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,texture);

    glBegin(GL_QUADS);

    glTexCoord2d(0,0);
    glVertex2f(mapX,mapY);

    glTexCoord2d(1,0);
    glVertex2f(mapX+800*2,mapY);

    glTexCoord2d(1,1);
    glVertex2f(mapX+800*2,mapY+600*2);

    glTexCoord2d(0,1);
    glVertex2f(mapX,mapY+600*2);
    glEnd();
    glDisable(GL_TEXTURE_2D);
}
void Hero()//(unsigned int texture)
{
    glColor4ub(0,255,0,255);
    glBegin(GL_QUADS);
    glVertex2f(x,y);
    glVertex2f(x+60,y);
    glVertex2f(x+60,y+60);
    glVertex2f(x,y+60);
    glEnd();
}
void Attribute()
{
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);//4even cvqt
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);//zelen cvqt
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);//sin cvqt
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);//plutnost na cvetovete
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,8);//koleminia na buffera (bitovete)
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);// zadavane na dulbo4ina
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);//ibira se dvoino buferirane

}
int main(int argc,char*args[])
{
    SDL_Init(SDL_INIT_EVERYTHING);//incqlizirame vseki element ot SDL
    Attribute();//vikame atributite
    SDL_WM_SetCaption("DJ RPG Game",NULL);//zadavame zaglavie
    SDL_SetVideoMode(w,h,32,SDL_OPENGL);//|SDL_FULLSCREEN);
    glClearColor(0,0,0,1);//iabirame cvqt
    glViewport(0,0,w,h);
    glShadeModel(GL_SMOOTH);//tova e za da izglejdat snimkite normalni iz4e se razmazvat

    glMatrixMode(GL_PROJECTION);//izbirame mod na matricata
    glLoadIdentity();//zarejdame q po default
    glOrtho(0,h,w,0,-1,1);
    glDisable(GL_DEPTH_TEST);//izbirame programat ada gleda 2 d
    glEnable( GL_BLEND );
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    unsigned int area = 0;
    area = loadTexture("map.png");
    bool running = true;
    SDL_Event event;//pravim slu4ei
    bool left=false,right=false,up=false,down=false;
    while(running)
    {
        while(SDL_PollEvent(&event))//hva6tame eventi
        {
            if(event.type == SDL_QUIT||event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)
            {
                running=false;
            }
            if( event.type == SDL_MOUSEBUTTONDOWN )
            {
                if( event.button.button == SDL_BUTTON_LEFT )
                {
                    if(event.button.x>=330)
                    {
                        right=true;
                    }
                    else if(event.button.x<330)
                    {
                        left=true;
                    }
                    else if(event.button.y>430)
                    {
                        down=true;
                    }
                    else if(event.button.y<430)
                    {
                        up=true;
                    }

                }
            }
            if( event.type == SDL_MOUSEBUTTONUP )
            {
                if( event.button.button == SDL_BUTTON_LEFT )
                {

                        right=false;

                        left=false;

                        down=false;

                        up=false;
                }
            }

        }
        if(right==true)
        {
            if(mapX!=event.button.x+mapX)
            {
                mapX-=10;
            }

        }
        if(left==true)
        {
            if(mapX!=event.button.x+mapX)
            {
                mapX+=10;
            }
        }
        if(up)
        {
            if(mapY!=event.button.y+mapY)
            {
                mapY+=10;
            }
        }
        if(down)
        {
           if(mapY!=event.button.y+mapY)
            {
                mapY-=10;
            }
        }
        glPushMatrix();
        glClear(GL_COLOR_BUFFER_BIT);

        Map(area);
        Hero();

        glPopMatrix();
        SDL_GL_SwapBuffers();
        SDL_Delay(1);
    }
    SDL_Quit();
}
